package com.tjetc.service.impl;

import com.tjetc.dao.BedDao;
import com.tjetc.dao.NotesDao;
import com.tjetc.dao.RecordDao;
import com.tjetc.dao.UserDao;
import com.tjetc.dao.impl.BedDaoImpl;
import com.tjetc.dao.impl.NotesDaoImpl;
import com.tjetc.dao.impl.RecordDaoImpl;
import com.tjetc.dao.impl.UserDaoImpl;
import com.tjetc.domain.Record;
import com.tjetc.domain.User;
import com.tjetc.service.RecordService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class RecordServiceImpl implements RecordService {
    private RecordDao recordDao = new RecordDaoImpl();
    private BedDao bedDao = new BedDaoImpl();
    private NotesDao notesDao = new NotesDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    @Override
    public int add(Record record) {
        try {
             recordDao.add(record);
             return recordDao.getMaxId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Record> listMyRecord(int uid) {
        try {
            return recordDao.listMyRecord(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Record getById(int id) {
        try {
            return recordDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(int did) {
        try {
            return recordDao.getCount(did);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Record> list(PageInfo<Record> pageInfo, int did) {
        try {
            return recordDao.list(pageInfo,did);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean outHs(int rid, int uid) {
        try {
            Db.startTransaction();
            // 4.计算住院中的病床费用
            // 4.1获取当前出院的时间
            java.util.Date date = new java.util.Date(123,8,22);
            // 4.2获取入院时间
            Record record = recordDao.getById(rid);
            Date inDate = record.getDate();
            long diffSeconds = Math.abs(date.getTime() - inDate.getTime());//相差毫秒数
            long diffDays = TimeUnit.DAYS.convert(diffSeconds, TimeUnit.MILLISECONDS);//相差天数
            // 4.3获取病人所住的病床费用
            double price = bedDao.findByUid(uid).getPrice();
            // 4.4计算总价
            double totalBedPrice = (diffDays+1) * price;
            System.out.println("totalBedPrice = " + totalBedPrice);
            User user = userDao.getById(uid);
            if (totalBedPrice > user.getAccount()){//代表床位费用大于用户余额
                return false;
            }else{
                // 4.5扣除用户余额
                user.setAccount(user.getAccount() - totalBedPrice);
                userDao.update(user);
                // 1.修改isLive字段
                recordDao.out(rid);
                // 2.修改病床状态
                bedDao.userOut(uid);
                // 3.删除护士记录
                notesDao.delNotes(uid);
                Db.commit();
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }

}
